<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once ('admin_controller.php');
class Picones extends Admin_controller {
  	
	function __construct(){
		parent::__construct();
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="help-block">', '</div>');
		$this->load->helper('form');
		$this->load->model('picon');
		$this->load->model('tipo_picon');
		$this->load->model('espacio');
	}
	
  	public function index(){
		$this->data['titulo'] = "Picones";
  		$this->data['picones'] = $this->picon->all_join();			
		$this->show('picones_view');
	}
	
	public function crear(){
		
		$this->data['titulo'] = "Crear picon";
		$this->data['controlador'] = 'picones/crear';
		$this->data['tipos'] = $this->tipo_picon->all();
		$this->data['espacios'] = $this->espacio->all();
		$this->data['estados'] = array('creado', 'suspendido', 'confirmado');
		
		$this->form_validation->set_rules('fecha', 'Fecha', 'trim|required|xss_clean');
		
		$ok = $this->form_validation->run();
		
		$this->data['picon']['id_espacio'] = $this->input->post('espacio');
		$this->data['picon']['id_tipo_picon'] = $this->input->post('tipo');
		$this->data['picon']['id_creador'] = $this->data['jugador']->id;
		$this->data['picon']['fecha'] = $this->input->post('fecha');
		$this->data['picon']['estado'] = $this->input->post('estado');
		$this->data['picon']['descripcion'] = $this->input->post('descripcion');
		
		if($ok){
			$this->picon->insert($this->data['picon']);
			redirect('picones', 'refresh');
		}else{
			$this->show('picon_form');
		}
	}
	
	function editar($id){
		
		$this->data['titulo'] = "editar picon";
		$this->data['controlador'] = 'picones/editar/' . $id;
		$this->data['tipos'] = $this->tipo_picon->all();
		$this->data['espacios'] = $this->espacio->all();
		$this->data['estados'] = array('creado', 'suspendido', 'confirmado');
		$this->data['picon'] = (array) $this->picon->id($id);
		
		$this->form_validation->set_rules('fecha', 'Fecha', 'trim|required|xss_clean');
			
		if($this->form_validation->run()){
			$this->data['picon']['id_espacio'] = $this->input->post('espacio');
			$this->data['picon']['id_tipo_picon'] = $this->input->post('tipo');
			$this->data['picon']['id_creador'] = $this->data['jugador']->id;
			$this->data['picon']['fecha'] = $this->input->post('fecha');
			$this->data['picon']['estado'] = $this->input->post('estado');
			$this->data['picon']['descripcion'] = $this->input->post('descripcion');
			
			$this->picon->update($this->data['picon'], $id);
			redirect('picones', 'refresh');
		}else{
			$this->show('picon_form');		
		}
	}
	function borrar($id){
		var_dump($id);
		$this->picon->delete($id);
		redirect('picones');
	}
}